<?php

namespace App\Http\Middleware;

use App\Facades\UserService;
use App\Facades\WeChatService;
use Closure;
use Sentinel;

class OAuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!Sentinel::check())
        {
            //链接包含参数 code
            if($request->has('code'))
            {
                UserService::add($request->code);
                return $next($request);
            }
            $redirectUri = urlencode($request->fullUrl());
            return redirect(WeChatService::getOAuthCode($redirectUri));
        }
        return $next($request);
    }
}
